package com.shen.accountbook.module.home.detailedfragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shen.accountbook.R;
import com.shen.accountbook.bean.NoteBeanByDay;
import com.shen.accountbook.module.home.booknote.BookNoteActivity;
import com.shen.accountbook.module.home.booknote.PhotoAdapter;
import com.shen.accountbook.module.home.booknote.RecyclerItemClickListener;
import com.shen.accountbook.stickyheader.StickyHeaderGridAdapter;
import com.shen.accountbook.utils.L;
import com.shen.accountbook.utils.StringUtils;
import com.shen.accountbook.widget.SwipeMenuView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.iwf.photopicker.PhotoPreview;

import static com.shen.accountbook.config.Constant.INTENT_BEAN;

/**
 * 悬浮头部项
 * 可侧滑删除
 */

public class DetailAdapter extends StickyHeaderGridAdapter {

   private Context mContext;

   private String baseUrl = "http://test.huishangsuo.cn";

   private List<NoteBeanByDay> mDatas;

   public void setDatas(List<NoteBeanByDay> datas) {
      this.mDatas = datas;
   }

   public DetailAdapter(Context context, List<NoteBeanByDay> datas){
      this.mContext = context;
      this. mDatas = datas;
   }


   @Override
   public int getSectionCount() {
      return mDatas == null ? 0 : mDatas.size();
   }

   @Override
   public int getSectionItemCount(int section) {
      return (mDatas == null || mDatas.get(section).getNoteList() == null) ? 0 : mDatas.get(section).getNoteList().size();
   }


   /* 填充标题布局 */
   @Override
   public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerType) {
      final View view = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.item_detail_header, parent, false);

      return new MyHeaderViewHolder(view);
   }

   /* 填充内容布局 */
   @Override
   public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemType) {
      final View view = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.item_detail_content, parent, false);
      return new MyItemViewHolder(view);
   }

   @Override
   public void onBindHeaderViewHolder(HeaderViewHolder viewHolder, int section) {
      final MyHeaderViewHolder holder = (MyHeaderViewHolder)viewHolder;

      holder.header_date.setText( mDatas.get(section).getTime());
      holder.header_money.setText(mDatas.get(section).getMoney() + "");
   }

   @Override
   public void onBindItemViewHolder(ItemViewHolder viewHolder, final int section, final int position) {
      final MyItemViewHolder holder = (MyItemViewHolder)viewHolder;
      final String label = mDatas.get(section).getNoteList().get(position).getThirdSortBean().getThird_sort_name();
      holder.item_title.setText(label);
      // Glide.with(mContext).load(baseUrl);
      holder.item_money.setText(mDatas.get(section).getNoteList().get(position).getMoney() + "");
      holder.mSwipeMenuView.setSwipeEnable(false); // sys系统的不做删除
//      if (mDatas.get(section).getNoteList().get(position).getId().startsWith("sys")){
//         holder.mSwipeMenuView.setSwipeEnable(false); // sys系统的不做删除
//      }else {
//         holder.mSwipeMenuView.setSwipeEnable(true);
//      }

      String image = mDatas.get(section).getNoteList().get(position).getImg();
      if(StringUtils.isNotEmpty(image)){
         final ArrayList<String> list = StringUtils.stringToList(image.split(","));
         initPhotoPicker(holder, list);
      }else {
         initPhotoPicker(holder, new ArrayList<String>());
      }

      holder.item_delete.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            final int section = getAdapterPositionSection(holder.getAdapterPosition());
            final int offset = getItemSectionOffset(section, holder.getAdapterPosition());

            mDatas.get(section).getNoteList().remove(offset);
            notifySectionItemRemoved(section, offset);
            Toast.makeText(holder.item_delete.getContext(), "删除--"+label, Toast.LENGTH_SHORT).show();
         }
      });


      holder.item_layout.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            // Toast.makeText(holder.item_layout.getContext(), "点击--"+label, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(mContext, BookNoteActivity.class);
            intent.putExtra(INTENT_BEAN, mDatas.get(section).getNoteList().get(position));
            mContext.startActivity(intent);
         }
      });



      // 禁止复用
      // holder.setIsRecyclable(false);
   }

   /**
    * 头部布局 -- 粘性头部/悬浮头部
    */
   public static class MyHeaderViewHolder extends HeaderViewHolder {
      /** 时间 */
      TextView header_date;
      /** 金钱（支出收入） */
      TextView header_money;

      MyHeaderViewHolder(View itemView) {
         super(itemView);
         header_date = itemView.findViewById(R.id.tv_date_itemdetailheader);
         header_money = itemView.findViewById(R.id.tv_money_itemdetailheader);
      }
   }

   public static class MyItemViewHolder extends ItemViewHolder {

      SwipeMenuView mSwipeMenuView;
      ConstraintLayout item_layout;

      /** 支出/收入 -- 类型 */
      TextView item_title;
      /** 支出/收入 -- 金额 */
      TextView item_money;
      /** 删除按钮 */
      Button item_delete;
      /** 支出/收入 -- 类型图片 */
      ImageView item_img;

      RecyclerView mRvImage;

      MyItemViewHolder(View itemView) {
         super(itemView);

         item_img = itemView.findViewById(R.id.iv_type_includeItemDetailContent);
         item_title = itemView.findViewById(R.id.tv_type_includeItemDetailContent);
         mRvImage = itemView.findViewById(R.id.rv_image_includeItemDetailContent);
         item_money = itemView.findViewById(R.id.tv_money_includeItemDetailContent);

         item_delete = itemView.findViewById(R.id.btn_delete_itemDetailContent);

         item_layout = itemView.findViewById(R.id.layout_includeItemDetailContent);
         mSwipeMenuView = itemView.findViewById(R.id.swipe_menu);
      }
   }


   private void initPhotoPicker(final MyItemViewHolder holder, final ArrayList<String> imageList) {
      PhotoAdapter photoAdapter = new PhotoAdapter(mContext, imageList, PhotoAdapter.TYPE_SHOW);

      holder.mRvImage.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL));
      holder.mRvImage.setAdapter(photoAdapter);

      holder.mRvImage.addOnItemTouchListener(new RecyclerItemClickListener(mContext,
              new RecyclerItemClickListener.OnItemClickListener() {
                 @Override
                 public void onItemClick(View view, int position) {
                    PhotoPreview.builder()
                            // .setPhotos(imageList)
                            .setPhotos(((PhotoAdapter)holder.mRvImage.getAdapter()).getPhotoPaths())
                            .setCurrentItem(position)
                            .setShowDeleteButton(false)
                            .start((Activity) mContext);
                 }
              }));
   }


}
